package proxy1;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: 582895699@qq.com
 * @Desctription: 动态代理
 * @Date: Created in 2021/12/14 15:57
 */
@Slf4j
public class DynamicProxy implements InvocationHandler {

    private final Object object;

    public DynamicProxy(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("前置动态代理");
        Object invoke = method.invoke(object, args);
        log.info("后置动态代理");
        return invoke;
    }


    public static void main(String[] args) {
        IBuyHouse buyHouse = new BuyHouseImpl();

        IBuyHouse proxyBuyHouse = (IBuyHouse) Proxy.newProxyInstance(
                IBuyHouse.class.getClassLoader(),
                new Class[]{IBuyHouse.class},
                new DynamicProxy(buyHouse));

        proxyBuyHouse.byeHouse();
    }
}
